﻿<?php
require_once("../config.php");
require_once(_SRC_."/dao/CategoryDao.php");

require_once(_SRC_."/dao/PageDao.php");
?>

<?php
	$categoryDao = new CategoryDao();
	$categories = $categoryDao->getCategories(0,1);

	$pageDao = new PageDao();
	
	if(isset($_GET["action"]) && $_GET["action"] == "deleteCategory"){
		$categoryDao->deleteCategory($_GET["id"]);
		header('Location: index.php?type=category');
	}else if(isset($_GET["action"]) && $_GET["action"] == "saveCategory"){
		for($i=1;$i<100;$i++){
			if(!isset($_POST["name_".$i])){
				break;
			}
			$category = new Category();
			if(isset($_POST["id_".$i]))
				$category->id = $_POST["id_".$i];
			$category->name = $_POST["name_".$i];
			if(isset($_POST["visible_".$i])){
				$category->visible = "1";
			}else{
				$category->visible = "0";
			}
			$category->pageId = $_POST["pageId_".$i];
			$category->level = 1;
			$category->parentId = 0;
			$category->order = 0;
			$categoryDao->updateCategory($category);
		}
		header('Location: index.php?type=category');
	}
?>
<form name="form" method="post" action="index.php?type=category&action=saveCategory">
	<table border=1 id="categoryTable">
			<tr>
				<td>顺序</td><td>名称</td><td>是否显示</td><td>页面</td><td>&nbsp;</td>
			</tr>
			<?php
			$i = 1;	
			foreach ($categories as $category){
				$id = $category->id;
				$pageId = $category->pageId;
				echo '<tr class="dataRow">';
				echo '<td class="number">'.$i.'</td>';
				echo '<td><input type="hidden" name="id_'.$id.'" value="'.$id.'"/>';
				echo '<input type="text" name="name_'.$id.'" value="'.$category->name.'"/></td>';
				if($category->visible == 1){
					echo '<td><input type="checkbox" name="visible_'.$id.'" checked="checked"/></td>';
				}else{
					echo '<td><input type="checkbox" name="visible_'.$id.'"/></td>';
				}	
					
				echo '<td><select class="pageIdSelect" name="pageId_'.$id.'">';
				$pages = $pageDao->getPages(); 
				foreach ($pages as $page){
					if($page->id == $pageId){
						echo '<option value='.$page->id.' selected>'.$page->name.'</option>';
					}else{
						echo '<option value='.$page->id.'>'.$page->name.'</option>';
					}	
				}
				echo '</select></td>';				
				echo '<td><a href="javascript:void(0)" id="deleteCategory"><img src="../static/images/delete.png"/></a></td>';
				echo '</tr>';		
				$i++;
			}
			?>
	</table>
	<p>
		<A href="#" id="addTopCategoryLink">添加一级栏目</A>
	</p>
	<div class="buttonBar"><input type="submit" id="saveButton" value="保存"></div>
</form>

<script>
(function(){
	$("#addTopCategoryLink").click(function(){
		var idStr = $(".number").last().html();
		var id = idStr?parseInt(idStr)+1:1; 

		var name = "name_"+id;
		var slug = "slug_"+id;
		var visible = "visible_"+id;
		
		$("#categoryTable").append('<tr class="dataRow">'
		+'<td>'+id+'</td>'
		+'<td><input type="text" name="'+name+'"/></td>'
		+'<td><input type="checkbox" name="'+visible+'" checked="checked"/></td>'
		+'<td></td>'
		+'</tr>');
		
	});
	
	$(".pageIdSelect").change(function(){
		var value = $(this).val();
		var available = true;
		$(".pageIdSelect").each(function(i, elem){
			if(value == $(elem).val()){
				
				available = false;
				return false;
			}
		});
	});
		
	$("#deleteCategory").click(function(){
		if(confirm("确定删除？")){
			location.href = "index.php?type=category&action=deleteCategory&id="+id;
		}
	});
	
	
})()

</script>